package com.atguigu.baku.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 地区工具类
 */
public class AreaUtils {
    /**
     * 根据地址获取省市id
     * @param address
     * @return
     */
    public static Map getAreaCityId(String address){
        Map map = new HashMap();
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        try {
            Matcher m = Pattern.compile(regex).matcher(address);
            //省份
            String area = "";
            //城市
            String city = "";
            while(m.find()){
                area = m.group("province").replace("省","").replace("市","");
                city = m.group("city").replace("市","");
            }
            if(area.contains("广西")){
                area = "广西";
            }else if(area.contains("内蒙古")){
                area = "内蒙古";
            }else if(area.contains("新疆")){
                area = "新疆";
            }else if(area.contains("宁夏")){
                area = "宁夏";
            }else if(area.contains("西藏")){
                area = "西藏";
            }
            //转换id
            map.put("area",area==null?"":area.trim());
            map.put("city",city==null?"":city.trim());
        }catch (Exception e){
            map.put("area","");
            map.put("city","");
        }
        return map;
    }


}
